package br.com.bancodecurriculos.controleacesso.gui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import br.com.bancodecurriculos.controleacesso.dominio.Usuario;
import br.com.bancodecurriculos.controleacesso.negocio.UsuarioServices;
import br.com.bancodecurriculos.util.exception.RegistroExistenteException;
import br.com.bancodecurriculos.util.gui.Form;

public class CadastrarUsuarioForm extends Form {

	private static final long serialVersionUID = 1L;

	/*
	 * private static final String LOGIN = "form.login"; private static final
	 * String SENHA = "form.senha"; private static final String CONFIRMA_SENHA =
	 * "form.confirma_senha"; private static final String CADASTRAR =
	 * "form.cadastrar";
	 */

	private static final String LOGIN = "Login *";
	private static final String SENHA = "Senha *";
	private static final String CONFIRMA_SENHA = "Confirme a senha *";
	private static final String CADASTRAR = "Cadastrar";

	private JLabel lblLogin;
	private JLabel lblSenha;
	private JLabel lblConfirmaSenha;
	private JTextField txtFLogin;
	private JPasswordField pwdSenha;
	private JPasswordField pwdConfirmaSenha;
	private JButton btnCadastrar;

	public CadastrarUsuarioForm() {
		super();
		this.init();
	}

	private void init() {
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setBounds(100, 100, 300, 164);
		try {
			this.loadComponents();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.acoes();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void loadComponents() throws IOException {
		this.lblLogin = new JLabel(LOGIN);
		this.lblLogin.setFont(super.getFont());
		this.lblLogin.setBounds(10, 11, 55, 21);
		this.getContentPane().add(this.lblLogin);

		this.txtFLogin = new JTextField();
		this.lblLogin.setLabelFor(this.txtFLogin);
		this.txtFLogin.setFont(super.getFont());
		this.txtFLogin.setBounds(160, 11, 124, 20);
		this.txtFLogin.setFocusable(true);
		this.getContentPane().add(this.txtFLogin);
		this.txtFLogin.setColumns(10);

		this.lblSenha = new JLabel(SENHA);
		this.lblSenha.setFont(super.getFont());
		this.lblSenha.setBounds(10, 43, 55, 14);
		this.getContentPane().add(this.lblSenha);

		this.pwdSenha = new JPasswordField();
		this.lblSenha.setLabelFor(this.pwdSenha);
		this.pwdSenha.setFont(super.getFont());
		this.pwdSenha.setBounds(160, 40, 124, 20);
		this.getContentPane().add(this.pwdSenha);

		this.lblConfirmaSenha = new JLabel(CONFIRMA_SENHA);
		this.lblConfirmaSenha.setFont(super.getFont());
		this.lblConfirmaSenha.setBounds(10, 68, 135, 21);
		this.getContentPane().add(this.lblConfirmaSenha);

		this.pwdConfirmaSenha = new JPasswordField();
		this.lblConfirmaSenha.setLabelFor(this.pwdConfirmaSenha);
		this.pwdConfirmaSenha.setFont(super.getFont());
		this.pwdConfirmaSenha.setBounds(160, 68, 124, 20);
		this.getContentPane().add(this.pwdConfirmaSenha);

		this.btnCadastrar = new JButton(CADASTRAR);
		this.btnCadastrar.setFont(super.getFont());
		this.btnCadastrar.setBounds(90, 101, 117, 25);
		this.getContentPane().add(this.btnCadastrar);
	}

	private void acoes() {
		this.btnCadastrar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (validarCampos()) {
					String login = txtFLogin.getText();
					String senha = String.valueOf(pwdSenha.getPassword());
					try {
						Usuario usuario = new Usuario();
						usuario.setLogin(login);
						usuario.setSenha(senha);
						UsuarioServices.getInstance().inserirUsuario(usuario);
						JOptionPane.showMessageDialog(
								CadastrarUsuarioForm.this,
								getCADASTRO_SUCESSO(), CADASTRAR,
								JOptionPane.INFORMATION_MESSAGE);
					} catch (ClassNotFoundException e1) {
						e1.printStackTrace();
					} catch (NoSuchAlgorithmException e1) {
						e1.printStackTrace();
					} catch (SQLException e1) {
						e1.printStackTrace();
					} catch (RegistroExistenteException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				} else {
					try {
						JOptionPane.showMessageDialog(
								CadastrarUsuarioForm.this,
								getCAMPOS_INVALIDOS(), CADASTRAR,
								JOptionPane.ERROR_MESSAGE);
					} catch (HeadlessException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
	}

	private boolean validarCampos() {
		if (txtFLogin.getText() == null
				|| super.getVAZIO().equals(txtFLogin.getText())) {
			return false;
		} else if (!this.isSenhaCorreta()) {
			return false;
		} else if (!this.validarTamanhoCampos()) {
			return false;
		}
		return true;
	}

	private boolean isSenhaCorreta() {
		if (String.valueOf(pwdSenha.getPassword()) == null
				|| super.getVAZIO().equals(
						String.valueOf(pwdSenha.getPassword()))) {
			return false;
		} else if (String.valueOf(pwdConfirmaSenha.getPassword()) == null
				|| super.getVAZIO().equals(
						String.valueOf(pwdConfirmaSenha.getPassword()))) {
			return false;
		} else if (!String.valueOf(pwdSenha.getPassword()).equals(
				String.valueOf(pwdConfirmaSenha.getPassword()))) {
			return false;
		}
		return true;
	}

	private boolean validarTamanhoCampos() {
		int tamanhoLogin = txtFLogin.getText().length();
		int tamanhoSenha = String.valueOf(pwdSenha.getPassword()).length();
		if (tamanhoLogin < 6 || tamanhoLogin > 20) {
			return false;
		} else if (tamanhoSenha < 6 || tamanhoSenha > 15) {
			return false;
		}
		return true;
	}

}
